package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;

public class UIShowData extends UIAbstractWindow
{

	public UIShowData(int id)
	{
		super(id);
		setBounds(0, 0, AbstractGameScreen.SCREEN_WIDTH, AbstractGameScreen.SCREEN_HEIGHT);
	}

	protected void paintContent(Graphics g)
	{
	     drawShowData(g);
	}

	protected void keyLogic()
	{
		
	}

	public static Vector s_showData = new Vector();//存放显示数据容器
	private static ShowData m_showdata;//显示数据的引用
	private final int m_line = 60;//区域框高度
	
	private void drawShowData(Graphics g){
	    if( m_showdata != null ){
	    	int clipX = g.getClipX();
			int clipY = g.getClipY();
			int clipW = g.getClipWidth();
			int clipH = g.getClipHeight();
			//Tools.debugPrintln("m_showdata.m_clipX="+m_showdata.m_clipX+";m_showdata.m_clipY="+m_showdata.m_clipY);
			g.setClip(m_showdata.m_clipX, m_showdata.m_clipY, AbstractGameScreen.SCREEN_WIDTH, m_line);
			m_showdata.draw(g);
			g.setClip(clipX, clipY, clipW, clipH);
	    }
	}
	
	public void logicShowData(){
		if( m_showdata == null && s_showData.size() == 0){
			return;
		}
		ShowData data = (ShowData)s_showData.elementAt(0);
		if( m_showdata == null ){
			m_showdata = data;
		}else{
			if( m_showdata.m_life ){
				if( data == m_showdata){
					m_showdata = null;
					s_showData.removeElementAt(0);
					s_showData.trimToSize();
				}	
			}else{
				m_showdata.run();
			}
		}
	}
	
	/** 重置显示数据 */
	public void resetShowData(){
		if( m_showdata != null){
			m_showdata = null;
		}
		if( s_showData.size() > 0 ){
			s_showData.removeAllElements();
		}
	}
	
	/** 接收显示数据 */
	public void recvShowData(DataInputStream dis) throws IOException {
		try{
			 int size = dis.readInt();
			 Tools.debugPrintln("显示数据 size="+size);
			 UIMain main  = (UIMain)UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MAIN);
			 if( main == null){
				 return;
			 }
			 ShowData data = new ShowData(size,main.getChatX(),main.getChatY(),m_line);
			 for(int i=0;i<size;i++){
				 byte type = dis.readByte();
				 int _colour = dis.readInt();
				 int _colourw = dis.readInt();
				 String _info = dis.readUTF();
				 int _num = dis.readInt();
				 //Tools.debugPrintln("i="+i+";type="+type+";_colour="+_colour+";info="+_info+";_num="+_num);
				 data = data.init(i, type, _colour, _colourw,_info, _num);
			 }
			 s_showData.addElement(data);
			Tools.debugPrintln("recvShowData 添加数据");
		}catch (Exception e) {
			Tools.debugPrintln("显示数据异常！"+e.toString());
		}
	}
}
